package com.jy.adscm.stock.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.dealer.bean.Dealer;
import com.jy.adscm.dealer.mapper.DealerMapper;
import com.jy.adscm.order.bean.JyProduct;
import com.jy.adscm.order.mapper.JyProductMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.stock.bean.JyInStorage;
import com.jy.adscm.stock.mapper.JyInStorageMapper;
import com.jy.adscm.stock.service.JyInStorageService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2019-01-23
 */
@Service
public class JyInStorageServiceImpl extends ServiceImpl<JyInStorageMapper, JyInStorage> implements JyInStorageService {
     @Autowired
    JyProductMapper jyProductMapper;
     @Autowired
    DealerMapper  dealerMapper;
     @Autowired
    JyCodeMapper jyCodeMapper;

    @Override
    public List<JyInStorage> selectAll(Token token) {
        List<JyInStorage> jyInStorages =new ArrayList<>();
        //厂家给全部的入库订单
         if (token.getUser().getUnitId()==0){
             Wrapper<JyInStorage> wrapper =new EntityWrapper<>();
             wrapper.orderBy("inStorage_time",false);
             jyInStorages = super.selectList(wrapper);
         } else {
             Wrapper<JyInStorage> wrapper =new EntityWrapper<>();
             wrapper.eq("dealer",token.getUser().getUnitId().toString());
             wrapper.orderBy("inStorage_time",false);
             jyInStorages = super.selectList(wrapper);
         }
        for (JyInStorage jyInStorage : jyInStorages) {
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyInStorage.getProduct(),null)){
                JyProduct jyProduct = jyProductMapper.selectById(jyInStorage.getProduct());
                jyInStorage.setProductName(jyProduct.getName());
                jyInStorage.setType(jyProduct.getType());
                jyInStorage.setTypeNum(jyProduct.getTypeNum());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyInStorage.getUnit(),null)){
                JyCode jyCode =new JyCode();
                jyCode.setType(6);
                jyCode.setCode(jyInStorage.getUnit());
                jyCode = jyCodeMapper.selectOne(jyCode);
                jyInStorage.setUnitName(jyCode.getName());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyInStorage.getDealer(),null)){
                Dealer dealer = dealerMapper.selectById(jyInStorage.getDealer());
                jyInStorage.setDealerName(dealer.getName());
            }
        }

        return jyInStorages;
    }
}
